package com.chaike.hsq;

import com.chaike.common.util.ValidateUC;
import com.chaike.hsq.exception.HuiShouQianCheckException;
import com.chaike.hsq.properties.HuiShouQianProperties;
import com.chaike.hsq.service.HuiShouQianService;
import com.chaike.hsq.service.impl.DefaultHuiShouQianService;
import com.chaike.hsq.storage.DefaultConfigStorageImpl;
import com.chaike.hsq.storage.HsqConfigStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 慧收钱自动配置.
 *
 * @author Luo
 * @version 1.0
 * @date 2021-12-31 10:50:09
 */
@Configuration
@EnableConfigurationProperties(HuiShouQianProperties.class)
public class HsqAutoConfiguration {

    /**
     * 注入慧收钱服务.
     *
     * @param properties 配置
     * @return 慧收钱服务
     */
    @Bean
    @ConditionalOnMissingBean(HuiShouQianService.class)
    @ConditionalOnProperty(prefix = HuiShouQianProperties.PREFIX, name = "enable", havingValue = "true")
    public HuiShouQianService huiShouQianService(final HuiShouQianProperties properties) {
        ValidateUC.notNull(properties, HuiShouQianCheckException.class, "慧收钱配置信息不能为空！");
        HsqConfigStorage configStorage = new DefaultConfigStorageImpl(properties);
        return new DefaultHuiShouQianService(configStorage);
    }

}
